package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import com.rla.domain.VipUserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.VipUserAddressBo;
import com.rla.domain.vo.VipUserAddressVo;
import com.rla.domain.VipUserAddress;
import com.rla.mapper.VipUserAddressMapper;
import com.rla.service.IVipUserAddressService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户收货地址Service业务层处理
 *
 * @author rla
 * @date 2025-08-07
 */
@RequiredArgsConstructor
@Service
public class VipUserAddressServiceImpl implements IVipUserAddressService {

    private final VipUserAddressMapper baseMapper;

    /**
     * 查询用户收货地址
     */
    @Override
    public VipUserAddressVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户收货地址列表
     */
    @Override
    public TableDataInfo<VipUserAddressVo> queryPageList(VipUserAddressBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VipUserAddress> lqw = buildQueryWrapper(bo);
        Page<VipUserAddressVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户收货地址列表
     */
    @Override
    public List<VipUserAddressVo> queryList(VipUserAddressBo bo) {
        LambdaQueryWrapper<VipUserAddress> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VipUserAddress> buildQueryWrapper(VipUserAddressBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VipUserAddress> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserId()), VipUserAddress::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getRealName()), VipUserAddress::getRealName, bo.getRealName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), VipUserAddress::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), VipUserAddress::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), VipUserAddress::getCity, bo.getCity());
        lqw.eq(bo.getCityId() != null, VipUserAddress::getCityId, bo.getCityId());
        lqw.eq(StringUtils.isNotBlank(bo.getDistrict()), VipUserAddress::getDistrict, bo.getDistrict());
        lqw.like(StringUtils.isNotBlank(bo.getLocationName()), VipUserAddress::getLocationName, bo.getLocationName());
        lqw.eq(StringUtils.isNotBlank(bo.getDetail()), VipUserAddress::getDetail, bo.getDetail());
        lqw.eq(bo.getPostCode() != null, VipUserAddress::getPostCode, bo.getPostCode());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), VipUserAddress::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), VipUserAddress::getLatitude, bo.getLatitude());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDefault()), VipUserAddress::getIsDefault, bo.getIsDefault());
        return lqw;
    }

    /**
     * 新增用户收货地址
     */
    @Override
    public Boolean insertByBo(VipUserAddressBo bo) {
        VipUserAddress add = BeanUtil.toBean(bo, VipUserAddress.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户收货地址
     */
    @Override
    public Boolean updateByBo(VipUserAddressBo bo) {
        VipUserAddress update = BeanUtil.toBean(bo, VipUserAddress.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editDefault(VipUserAddressBo bo) {

        //修改该用户下所有地址为非默认地址
        VipUserAddress update = BeanUtil.toBean(bo, VipUserAddress.class);
        int i = baseMapper.update(null, Wrappers.lambdaUpdate(VipUserAddress.class)
                .set(VipUserAddress::getIsDefault, "0")
                .eq(VipUserAddress::getUserId, bo.getUserId()));
        int j = baseMapper.updateById(update);
        return i>0 && j>0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VipUserAddress entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户收货地址
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
